//
//  FMDBCollectionManager.m
//  takeout
//
//  Created by Mac on 12-4-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "FMDBCollectionManager.h"
#import "FMDatabase.h"
#import "FMDBManager.h"
#import "Collection.h"
#import "Supplier.h"
@implementation FMDBCollectionManager
//获取所有收藏店铺
-(NSArray *) getAllCollection{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM collections"];
    NSMutableArray *collections=[NSMutableArray arrayWithCapacity:2];
    while ([rs next]) {
        Collection *collection=[[Collection alloc]init];
        collection.collectionId=[rs intForColumn:@"id"];
        collection.supplierId=[rs intForColumn:@"supplierid"];
        collection.supplierName=[rs stringForColumn:@"name"];
        collection.imgUrl=[rs stringForColumn:@"picture"];
        [collections addObject:collection];
        [collection release];
    }    
    [rs close];
    [fmdbManager closeDataBase];
    [fmdbManager release];
    return collections;
}
//插入收藏店铺
-(BOOL) insertCollection:(Supplier *)supplier{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM collections where supplierid=?",[NSNumber numberWithInt:supplier.supplierId]];
    if([rs next]){
        [fmdbManager closeDataBase];
        [fmdbManager release];
        return false;
    }
    else{
        [db executeUpdate:@"INSERT INTO collections(supplierid,name,picture) values(?,?,?)",[NSNumber numberWithInt:[supplier supplierId]],supplier.supplierName,supplier.imgUrl];
        [fmdbManager closeDataBase];
        [fmdbManager release];
        return true;
    }
    return false;
}


@end
